package wicketcool.converter;

import japa.parser.ASTHelper;
import japa.parser.ast.expr.AnnotationExpr;
import japa.parser.ast.expr.Expression;
import japa.parser.ast.expr.MemberValuePair;
import japa.parser.ast.expr.NormalAnnotationExpr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import wicketcool.model.WCLAnnotation;
import wicketcool.model.value.WCLValue;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * User: rabbit
 * Date: Aug 3, 2009
 */
@Component
public class DefaultAnnotationsConverter implements AnnotationsConverter {

    @Autowired
    private DefaultValueConverter valueConverter;

    public List<AnnotationExpr> createAnnotationsExpr(List<WCLAnnotation> annotations) {
        List<AnnotationExpr> annotationExprs = new ArrayList<AnnotationExpr>();
        for (WCLAnnotation annotation : annotations) {

            List<MemberValuePair> memberValuePairs = new ArrayList<MemberValuePair>();
            Map<String, WCLValue> attributes = annotation.getAttributes();
            for (String name : attributes.keySet()) {
                Expression expression = valueConverter.createExpression(attributes.get(name));
                MemberValuePair memberValuePair = new MemberValuePair(name, expression);
                memberValuePairs.add(memberValuePair);
            }

            annotationExprs.add(
                    new NormalAnnotationExpr(ASTHelper.createNameExpr(annotation.getName()),
                            memberValuePairs));

        }
        return annotationExprs;
    }


}
